package com.rickpan.entity;

import jakarta.persistence.*;
import lombok.Data;

import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * 支付订单实体类
 */
@Entity
@Table(name = "payment_orders", indexes = {
    @Index(name = "idx_user_id", columnList = "userId"),
    @Index(name = "idx_order_no", columnList = "orderNo"),
    @Index(name = "idx_status", columnList = "status"),
    @Index(name = "idx_created_at", columnList = "createdAt"),
    @Index(name = "idx_expires_at", columnList = "expiresAt"),
    @Index(name = "idx_user_status", columnList = "userId, status")
})
@Data
public class PaymentOrder {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    /**
     * 用户ID
     */
    @Column(nullable = false)
    private Long userId;

    /**
     * 订单号
     */
    @Column(unique = true, nullable = false, length = 64)
    private String orderNo;

    /**
     * 套餐类型
     */
    @Enumerated(EnumType.STRING)
    @Column(nullable = false)
    private PlanType planType;

    /**
     * 支付金额
     */
    @Column(nullable = false, precision = 10, scale = 2)
    private BigDecimal amount;

    /**
     * 订单状态
     */
    @Enumerated(EnumType.STRING)
    @Column(nullable = false)
    private PaymentStatus status = PaymentStatus.PENDING;

    /**
     * 支付方式
     */
    @Enumerated(EnumType.STRING)
    @Column(nullable = false)
    private PaymentMethod paymentMethod = PaymentMethod.ALIPAY;

    /**
     * 第三方交易号
     */
    @Column(length = 64)
    private String tradeNo;

    /**
     * 创建时间
     */
    @Column(nullable = false, updatable = false)
    private LocalDateTime createdAt;

    /**
     * 支付时间
     */
    private LocalDateTime paidAt;

    /**
     * 订单过期时间
     */
    private LocalDateTime expiresAt;

    /**
     * 支付回调数据
     */
    @Column(columnDefinition = "JSON")
    private String callbackData;

    /**
     * 备注信息
     */
    @Column(length = 500)
    private String remark;

    @PrePersist
    protected void onCreate() {
        createdAt = LocalDateTime.now();
    }

    /**
     * 套餐类型枚举
     */
    public enum PlanType {
        MONTHLY_VIP("月度VIP"),
        YEARLY_VIP("年度VIP");

        private final String description;

        PlanType(String description) {
            this.description = description;
        }

        public String getDescription() {
            return description;
        }
    }

    /**
     * 支付状态枚举
     */
    public enum PaymentStatus {
        PENDING("待支付"),
        PAID("已支付"),
        FAILED("支付失败"),
        CANCELLED("已取消");

        private final String description;

        PaymentStatus(String description) {
            this.description = description;
        }

        public String getDescription() {
            return description;
        }
    }

    /**
     * 支付方式枚举
     */
    public enum PaymentMethod {
        ALIPAY("支付宝"),
        WECHAT("微信支付"),
        MANUAL("手动充值");

        private final String description;

        PaymentMethod(String description) {
            this.description = description;
        }

        public String getDescription() {
            return description;
        }
    }
}